package com.jfinal.ext.ueditor.result;

import com.jfinal.ext.ueditor.entity.FileInfo;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

/**
 * @author Timor
 * @version 1.0
 * @date 2023/01/06 14:10:19
 */
public class ImageUploadResult extends UploadResult {
    /**
     * 压缩后的图片列表：
     *      Map\<图片尺寸: String, 图片url：String>
     */
    private Map<String, String> compressed = null;


    protected ImageUploadResult(boolean success) {
        super(success);
    }

    protected ImageUploadResult(@NotNull String message) {
        super(message);
    }

    public static ImageUploadResult failure() {
        return new ImageUploadResult(false);
    }

    public static ImageUploadResult failure(@NotNull String message) {
        return new ImageUploadResult(message);
    }

    public static ImageUploadResult success() {
        return new ImageUploadResult(true);
    }

    public static ImageUploadResult success(@NotNull String url,
                                       @NotNull String newName,
                                       @NotNull String originalName) {
        final ImageUploadResult result = success();

        result.setUrl(url);
        result.setName(newName);
        result.setTitle(originalName);
        result.setOriginal(originalName);

        return result;
    }

    public static ImageUploadResult success(@NotNull FileInfo fileInfo) {
        final ImageUploadResult result = success();

        result.setUrl(fileInfo.getUrl());
        result.setName(fileInfo.getNewName());
        result.setTitle(fileInfo.getOriginalName());
        result.setOriginal(fileInfo.getOriginalName());

        return result;
    }

    public Map<String, String> getCompressed() {
        return compressed;
    }

    public void setCompressed(Map<String, String> compressed) {
        if (null != compressed && !compressed.isEmpty()) {
            this.compressed = Map.copyOf(compressed);
        }
    }
}
